package com.henry.customviewstudy.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.henry.customviewstudy.R;

/**
 * Created by henry on 2018/4/5.
 */

public class CustomImageView extends View {
    // 图片
    private Bitmap mImage;
    // 图片缩放形式
    private int mImageScaleType;
    // 文字
    private String mTitleText;
    // 文字大小
    private int mTitleTextSize;
    // 文字颜色
    private int mTitleTextColor;
    // 用来画图片使用
    private Rect mRect;
    // 用来画文字使用
    private Rect mTextBound;
    // 画笔
    private Paint mPaint;
    // 宽
    private int mWidth;
    // 高
    private int mHeight;
    // 缩放形式
    private int IMAGE_SCALE_FITXY = 0;
    private int IMAGE_SCALE_CENTER = 1;

    public CustomImageView(Context context) {
        this(context, null);
    }

    public CustomImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获得自定义属性
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyleAttr, 0);
        int indexCount = a.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int arr = a.getIndex(i);
            switch (arr) {
                case R.styleable.CustomImageView_image:
                    mImage = BitmapFactory.decodeResource(getResources(), a.getResourceId(arr, 0));
                    break;
                case R.styleable.CustomImageView_imageScaleType:
                    mImageScaleType = a.getInt(a.getIndex(arr), 0);
                    break;
                case R.styleable.CustomImageView_imageTitleText:
                    mTitleText = a.getString(arr);
                    break;
                case R.styleable.CustomImageView_imageTitleTextColor:
                    mTitleTextColor = a.getColor(a.getIndex(arr), 0);
                    break;
                case R.styleable.CustomImageView_imageTitleTextSize:
                    mTitleTextSize = a.getDimensionPixelSize(arr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
            }
        }
        a.recycle();
        mRect = new Rect();
        mPaint = new Paint();
        mTextBound = new Rect();
        mPaint.setTextSize(mTitleTextSize);
        //计算了描绘字体所需要的范围
        if (!TextUtils.isEmpty(mTitleText)) {
            mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mTextBound);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //设置宽度
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            mWidth = specSize;
        } else {
            //图片希望的宽
            int mImageWidth = getPaddingLeft() + getPaddingRight() + mImage.getWidth();
            //文字希望的宽
            int mTextWidth = getPaddingLeft() + getPaddingRight() + mTextBound.width();
            if (specMode == MeasureSpec.AT_MOST) {
                int des = Math.max(mImageWidth, mTextWidth);
                //有可能占满全屏，保险起见，取小的
                mWidth = Math.min(des, specSize);
            }
        }
        //设置高度
        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            mHeight = specSize;
        } else {
            int des = getPaddingBottom() + getPaddingTop() + mImage.getHeight() + mTextBound.height();
            //有可能全屏，取小的
            if (specMode == MeasureSpec.AT_MOST) {
                mHeight = Math.min(des, specSize);
            }
        }
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        //边框
        mPaint.setStrokeWidth(4);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLUE);
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);

        mRect.left = getPaddingLeft();
        mRect.right = mWidth - getPaddingRight();
        mRect.top = getPaddingTop();
        mRect.bottom = mHeight - getPaddingBottom();

        mPaint.setColor(mTitleTextColor);
        mPaint.setStyle(Paint.Style.FILL);

        /**
         * 当前设置的宽度小于字体的需要的宽度,将字体改为xxx...
         */
        if (mTextBound.width() > mWidth) {
            TextPaint textPaint = new TextPaint(mPaint);
            String msg = TextUtils.ellipsize(mTitleText, textPaint, (float) mWidth - getPaddingRight() - getPaddingLeft(), TextUtils.TruncateAt.END).toString();
            canvas.drawText(msg, getPaddingLeft(), mHeight - getPaddingBottom(), mPaint);
        } else {
            //正常情况下将字体居中
            canvas.drawText(mTitleText, mWidth / 2 - mTextBound.width() / 2, mHeight - getPaddingBottom(), mPaint);
        }
        //取消掉使用掉的地方，为图片流出空间
        mRect.bottom -= mTextBound.height();
        if (mImageScaleType == IMAGE_SCALE_FITXY) {
            canvas.drawBitmap(mImage, null, mRect, mPaint);
        } else {
            //居中，计算居中的矩形范围
            mRect.left = mWidth / 2 - mImage.getWidth() / 2;
            mRect.right = mWidth / 2 + mImage.getWidth() / 2;
            mRect.top = (mHeight - mTextBound.height()) / 2 - mImage.getHeight() / 2;
            mRect.bottom = (mHeight - mTextBound.height()) / 2 + mImage.getHeight() / 2;
            canvas.drawBitmap(mImage, null, mRect, mPaint);
        }
    }
}
